package com.android.taski;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.DatePicker;

public class ReminderEditActivity extends Activity {
	private Button mDateButton;
	private Button mTimeButton;
	private Calendar mCalendar;
	private static final int DATE_PICKER_DIALOG = 0;
	private static final int TIME_PICKER_DIALOG = 1;
	private static final String DATE_FORMAT="yyyy-MM-dd";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mCalendar = Calendar.getInstance();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reminder_edit);
		mDateButton = (Button) findViewById(R.id.reminder_date);
		mTimeButton = (Button) findViewById(R.id.reminder_time);
		registerButtonListenerAndSetDefaultText();
	}

	private void registerButtonListenerAndSetDefaultText() {
		// TODO Auto-generated method stub
		mDateButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DATE_PICKER_DIALOG);
			}
		});
		mTimeButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub	
				showDialog(TIME_PICKER_DIALOG);
			}
		});
		updateDateButtonText();
		updateTimeButtonText();
	}
	private void updateTimeButtonText() {
		// TODO Auto-generated method stub
		
	}

	private void updateDateButtonText() {
		// TODO Auto-generated method stub
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		String dateForButton = dateFormat.format(mCalendar.getTime());
		mDateButton.setTag(dateForButton);
	}

	protected Dialog onCreateDialog(int id) {
		switch(id) {
		case DATE_PICKER_DIALOG:return showDatePicker();
		}
		return super.onCreateDialog(id);
	}

	private DatePickerDialog showDatePicker() {
		// TODO Auto-generated method stub
		DatePickerDialog datePicker = new DatePickerDialog(ReminderEditActivity.this, new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				mCalendar.set(Calendar.YEAR, year);
				mCalendar.set(Calendar.MONTH, monthOfYear);
				mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				updateDateButtonText();
			}
		}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
		return datePicker;
	}
}
